/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.home2;

import android.app.Activity;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.isisufes.R;
import android.isisufes.util.ServerCom;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ContactQueryShow {
	Activity a;
	LayoutInflater viewInflate;
	ListView v;
	SQLiteDatabase myBase;
	String values[][];
	
	public ContactQueryShow(String[][] search, Activity a, SQLiteDatabase myBase){
		this.myBase=myBase;
		this.a=a;
		((Home2)a).previousScreen=Home2.CONT;
		viewInflate = LayoutInflater.from(a);
		a.setContentView(R.layout.search_result_cont);
		this.v=(ListView)a.findViewById(R.id.search_result_list_view);
		SetButs();
		PutIntoList(search);
		values=search;
	}

	private void SetButs() {

		ImageButton c =(ImageButton)a.findViewById(R.id.search_result_join_button);
		c.setOnClickListener(new OnClickListener(){
				public void onClick(View arg0) {
					for (int i = 0; i < v.getAdapter().getCount(); i++) {
						Log.d("Line",i+"");
						
						RelativeLayout r = (RelativeLayout) v.getAdapter().getView(i, null, null);
						CheckBox cb = (CheckBox) r.getChildAt(0);
						if (cb.isChecked()) {
							Log.d("ADDING",ContactQueryShow.this.values[i][1]);
							ServerCom.AddContact(ContactQueryShow.this.values[i][1]);
							try {
								myBase.execSQL("INSERT INTO contacts VALUES ("+ContactQueryShow.this.values[i][1]+",'"+ContactQueryShow.this.values[i][0]+"','"+((Home2)a).getUsername()+"');");
							} catch (SQLException e) {
								Log.d("SQLERROR","id repetida"+ContactQueryShow.this.values[i][1]);
							}
						}
					}
				}			
			});
	}

	private void PutIntoList(String [][]search) {
		add(search);
	}
	
	public void add(String[][] toAdd) {
		RelativeLayout[] rl = new RelativeLayout[toAdd.length];
		for (int i = 0; i < toAdd.length; i++) {
			RelativeLayout r = (RelativeLayout) viewInflate.inflate(
					R.layout.line_template, null);
			((TextView) r.getChildAt(1)).setText(toAdd[i][0]);
			((TextView) r.getChildAt(2)).setText("User id: "+toAdd[i][1]);
			rl[i] = r;
		}
		v.setAdapter(new MyListAdapter(rl));

	}
	
	final class MyListAdapter extends BaseAdapter {
		RelativeLayout[] item;

		public MyListAdapter(RelativeLayout[] v) {
			item = v;
		}

		public Object getItem(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return item[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public int getCount() {
			return item.length;
		}

	}
	
}
